﻿using System;
using System.Data;
using System.Windows.Forms;
using AT_UHF_NET;
using System.Collections.Generic;
using ceTerm.OverProduction;
using System.IO;
using System.Drawing;

namespace ceTerm
{
    public partial class ListRfId : XForm
    {  
        public ListRfId()
        {
            InitializeComponent();

            pnlSinyalGucu.Visible = true;
            this.WindowState = FormWindowState.Maximized;
            this.KeyPreview = true;
            timer.Interval =  (int)Settings.OkumaSuresiMs;

            if (Main.ceWS.OPGetFails(Main.Password))
            {
                MessageBox.Show("RFID yazılmamış toplar var. Lütfen rafa yerleştirme yapın ya da IT ekibine haber verin.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                this.Close();
            }
            else
            {
                Start();
            }
        }

        private void ShowInfo(string info)
        {
            lblStatus.Text = info;
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F18: // AT570, AT870 Gun Trigger 
                case Keys.F19: // AT570, AT870 Gun Trigger                                            
                  
                    break;
                default:
                    break;
            }
            base.OnKeyDown(e);
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F18: // AT570, AT870 Gun Trigger 
                case Keys.F19: // AT570, AT870 Gun Trigger                                         
                    count = 0;
                    timer.Enabled = true;
                    break;
                default:
                    break;
            }
            base.OnKeyUp(e);
        }
 
        //Read Events
        int count = 0;
        int lastEpc = -1;
        public override void GetAccessEPC(string EPC)
        {
            if (working)
                return;

            string[] temp = EPC.Split(",".ToCharArray());
            int EPCint = intParse(temp[0].Substring(4, temp[0].Length - 4));
            if (EPCint < 1)
                return;

            //if (BarkodTipi(EPCint) != barkodTipleri.Top)
            //{
            //    ShowInfo("Geçerli bir top no değil !");                
            //    return;
            //}
            //ShowInfo("");

            //string name = Main.ceWS.OPGetRollDetail(EPCint);
            //if (name != "")
            {
                if (lastEpc != EPCint)
                {
                    count++;
                    lastEpc = EPCint;
                    lblArticle.Text = "";//name;
                    lblDonenRollId.Text = EPCint.ToString();                
                }
            }
        }

        int testRollID = 20510;
        private void btnTest_Click(object sender, EventArgs e)
        {
            testRollID++;
            GetAccessEPC("0000" + testRollID.ToString());

            count = 0;
            timer.Enabled = true;
        }
     
        
 

        bool working = false;
        private void timer_Tick(object sender, EventArgs e)
        {
            timer.Enabled = false;            
            //Terminal.StopRFID(); //StopRead idi!
            working = true;
            if (count == 0 && lblDonenRollId.Text != "")
            {

                DataSet ds = Main.ceWS.GetTopDetay(Main.Password,int.Parse(lblDonenRollId.Text), 2);
                DataRow dr = ds.Tables[0].Rows[0];
                int donenRollId = int.Parse(lblDonenRollId.Text);
                string artikel = dr["Artikel"].ToString();
                string brand = dr["Brand"].ToString();
                byte[] bytes = (byte[])ds.Tables[1].Rows[0]["Photo"];
                MemoryStream stream = new MemoryStream(bytes);
                Bitmap image = new Bitmap(stream);
               
                
                if ( (int)dr["ShelfId"] != 1 )
                {
                    MessageBox.Show("Çıkış yapılmamış bir top imalat fazlası olarak giriş yapılamaz !");
                    Terminal.PlayFail();
                }
                else
                {
                    Terminal.PlaySuccess();
                    ReadWriteRFID rw = new ReadWriteRFID(donenRollId, artikel, brand, image);
                    rw.ShowDialog();
                }
                
            }
            else
            {
                ShowInfo("Ortamda "+ count+1 + " adet RFID var.");
                Terminal.PlayFail();
            }

            Start();
        }

        public override void lDetay_Click(object sender, EventArgs e)
        {
            Browse("TopDetay.aspx?TopId=" + lblDonenRollId.Text, true, "Top Detayı");
        }

            private void Start()
            {
                lblDonenRollId.Text = "";
                lastEpc = -1;
                Terminal.StartRFID();
                Terminal.SetActivatedForm(this);
                Terminal.RFID.SET_PowerControl(20);
                ShowInfo("");
                working = false;
            }

         
    }
}